package src.com.unapec.seguridad.login.model;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class Decryption {

	private String algorithm = "DESede";
	private Cipher cipher = null;
	private byte[] encryptionBytes = null;
	private SecretKey sKey = null;

	public Decryption() {

		try {

			sKey  = KeyGenerator.getInstance("DES").generateKey();
			cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE, sKey);


		} catch (NoSuchPaddingException e) {

			e.printStackTrace();


		} catch (NoSuchAlgorithmException e) {

			e.printStackTrace();

		} catch (InvalidKeyException e) {

			e.printStackTrace();

		}
	}

	public String decrypt(String str) {

		String decryptedData = null;

		try {
			// Decode base64 to get bytes
			byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);

			// Decrypt
			byte[] utf8 = cipher.doFinal(dec);

			// Decode using utf-8
			decryptedData = new String(utf8, "UTF8");

		} catch (javax.crypto.BadPaddingException e) {
			
			e.printStackTrace();
			
		} catch (IllegalBlockSizeException e) {
			
			e.printStackTrace();
					
		} catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			
		
		} catch (java.io.IOException e) {
		
			e.printStackTrace();
							
		}
		
		return decryptedData;
	}

	public static void main(String[] args) {

		Decryption d = new Decryption();
		System.out.println(d.decrypt("+BgI8U2nuDEzc1FhpSkuhg=="));


	}


}
